

MapUtils = {}

MapUtils.openTomeCallback = nil
MapUtils.toggleTomeCallback = nil

function MapUtils.RegisterOpenTomeCallback( callbackFunction )

    if callbackFunction == nil then
        ERROR(L"Attempting to set Open Tome Callback function to nil!")
    else
        MapUtils.openTomeCallback = callbackFunction
    end
    
end

function MapUtils.RegisterToggleTomeCallback( callbackFunction )

    if callbackFunction == nil then
        ERROR(L"Attempting to set Toggle Tome Callback function to nil!")
    else
        MapUtils.toggleTomeCallback = callbackFunction
    end
    
end

function MapUtils.ClickMap( mapDisplay, points )

    if MapUtils.openTomeCallback ~= nil then

        for index, ptIndex in ipairs( points ) do    
         
            local pointData = GetMapPointData( mapDisplay, ptIndex )
            
            if( pointData.pointType == SystemData.MapPips.QUEST_AREA) then
            
                if( WindowGetShowing( "TomeWindow" ) == false  ) then	   	    
                    MapUtils.toggleTomeCallback()
                else
                    --bring to front even if it's behind something now
                    MapUtils.toggleTomeCallback()
                    MapUtils.toggleTomeCallback()
                end
                MapUtils.openTomeCallback( pointData.id )
                
                break
            end 

        end   
    
    end
    
end


-- Updates a CityRating container.
function MapUtils.UpdateCityRatingWindow( cityId, windowName )

    -- Show the Rating Stars    
    local sliceName = "star-order"
    if( cityId == GameData.CityId.CHAOS )
    then
        --DEBUG( L"Destruction"..controlledBy )
        sliceName = "star-destruction"
    end

    local cityRating = GetCityRatingForCityId( cityId )

    for rating = 1, GameDefs.NUM_CITY_RANKS 
    do
        local showStar = rating <= cityRating
        WindowSetShowing( windowName.."Star"..rating, showStar )
        
        if( showStar )
        then
            DynamicImageSetTextureSlice( windowName.."Star"..rating, sliceName )
       end
   end
   
   
      
end

-- Given the zone number of a peaceful city, get the zone number of its contested version
function MapUtils.GetContestedCityZoneFromPeacefulZone( peacefulZoneId )
    local cityId = GameDefs.PeacefulCityZoneIDs[peacefulZoneId]
    if (cityId ~= nil) then
        for possibleZoneId, possibleCityId in pairs(GameDefs.ZoneCityIds) do
            -- The contested city has the same city ID number as the peaceful city but a different zone number
            if ((possibleCityId == cityId) and (possibleZoneId ~= peacefulZoneId)) then
                return possibleZoneId
            end
        end
    end
    
    return 0
end
